package com.ccx.coachsite.util;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.ccx.coachsite.util.weibo.tencent.service.Weibo;
import com.ccx.coachsite.util.weibo.tencent.ui.QQMainActivity;
import com.ccx.coachsite.util.weibo.tencent.util.TextUtil;

public class OpenShareQQWeiBo {
	
	private Context mContext;
	private ProgressDialog dialog;
	private SharedPreferences data;
	private String shareAppName;
	private String mUserId;
	
	public OpenShareQQWeiBo(Context context){
		mContext = context;
		
		dialog = new ProgressDialog(mContext);
		dialog.setIndeterminate(false);
		dialog.setCancelable(true);
		
		data = mContext.getSharedPreferences("QQWeiBo", 0);
	}
	
	
	public void shareQQWeiBo( String targetPackage, String appName, String userId){
		
		shareAppName = appName;
		mUserId = userId;
		
		//从配置文件中读取accessToken和accessTokenSecret
		String accessToken = data.getString("accessToken_"+mUserId, "");
		String accessTokenSecret = data.getString("accessTokenSecret_"+mUserId, "");

		//当accessToken和accessTokenSecret都不为空时
		if (!TextUtil.isEmpty(accessToken)&&!TextUtil.isEmpty(accessTokenSecret)){
			Weibo weibo = new Weibo();
			String result = weibo.add(accessToken,accessTokenSecret, "json", shareAppName, "127.0.0.1");
			displayResult(result);
		}else{
			Intent intentQQ = new Intent();
			intentQQ.setClass(mContext, QQMainActivity.class);
			intentQQ.putExtra("mUserId", mUserId);
			mContext.startActivity(intentQQ);
		}
	}
	
	private void displayResult(String result){
		if (result.indexOf("ok")!=-1) {
			Toast.makeText(mContext, OpenShareTool.getShareDesc(mContext)+"成功", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(mContext, OpenShareTool.getShareDesc(mContext)+"失败", Toast.LENGTH_SHORT).show();
		}
	}
}
